package indoor.nexd.com.nexdmap.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.PointF;
import android.os.Handler;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import app.xu.com.listtext.listener.OnTextItemClickListener;
import app.xu.com.listtext.view.ListTextView;
import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnItemClick;
import butterknife.OnTextChanged;
import cn.nexd.map.rendering.POIData;
import cn.nexd.map.rendering.SVGMapViewClickListener;
import cn.nexd.map.rendering.SVGMapViewListener;
import cn.nexd.map.rendering.SVGMapViewOtherClickListener;
import cn.nexd.map.rendering.core.SVGMapBaseOverlay;
import cn.nexd.map.rendering.core.componet.SVGMapView;
import cn.nexd.map.rendering.core.componet.data.Feature;
import cn.nexd.map.rendering.core.componet.data.FloorsFeature;
import indoor.nexd.com.nexdmap.R;
import indoor.nexd.com.nexdmap.adapter.GridViewAdapter;
import indoor.nexd.com.nexdmap.adapter.SearchResultAdapter;
import indoor.nexd.com.nexdmap.application.MyApplication;
import indoor.nexd.com.nexdmap.model.FloorModel;
import indoor.nexd.com.nexdmap.model.GridViewModel;
import indoor.nexd.com.nexdmap.model.MallModel;
import indoor.nexd.com.nexdmap.model.RecordBean;
import indoor.nexd.com.nexdmap.nexdmapoverly.CompassOverlay;
import indoor.nexd.com.nexdmap.nexdmapoverly.HighLightOverlay;
import indoor.nexd.com.nexdmap.nexdmapoverly.MarkerOverlay;
import indoor.nexd.com.nexdmap.nexdmapoverly.MorePointOverly;
import indoor.nexd.com.nexdmap.presenter.NexdMapPresenter;
import indoor.nexd.com.nexdmap.ui.BaseActivity;
import indoor.nexd.com.nexdmap.ui.inte.INexdMapActivity;
import indoor.nexd.com.nexdmap.util.ProgressDialogUtils;
import indoor.nexd.com.nexdmap.util.SharedPreferencesUtil;
import indoor.nexd.com.nexdmap.view.MyScrollView;

/**
 * NexdSvgMapView，用于显示室内地图信息
 * Created by wangxu on 2015/12/4.
 */
public class NexdMapActivity extends BaseActivity<NexdMapPresenter> implements SVGMapViewClickListener, INexdMapActivity, OnTextItemClickListener, SVGMapViewListener, SVGMapViewOtherClickListener {
    public static final int LOCATION = 0;
    public static final int MARKER = 1;
    private static final String TAG = NexdMapActivity.class.getSimpleName();
    public static boolean IS_LOCATIONING = false;
    @Bind(R.id.svg_map)
    SVGMapView nexdMap;
    @Bind(R.id.type_record)
    MyScrollView typeRecord;
    @Bind(R.id.back)
    ImageButton back;
    @Bind(R.id.gotothere)
    Button goToThere;
    @Bind(R.id.start_position)
    Button startPosition;
    @Bind(R.id.end_position)
    Button endPosition;

    @Bind(R.id.poi_name)
    TextView poiName;
    @Bind(R.id.popuLayout)
    FrameLayout popuLayout;
    @Bind(R.id.search_layout)
    FrameLayout searchLayout;
    @Bind(R.id.map_layout)
    RelativeLayout mapLayout;
    @Bind(R.id.search_content)
    AppCompatEditText searchContent;
    @Bind(R.id.search_clear)
    ImageButton searchClear;
    @Bind(R.id.search_go)
    ImageView searchGoBtn;
    @Bind(R.id.ltv_navigate)
    ListTextView ltvNavigate;
    @Bind(R.id.gridview)
    GridView gridview;
    @Bind(R.id.tlv_record)
    ListView tlvRecoed;
    @Bind(R.id.search_result)
    ListView searchResult;
    @Bind(R.id.map_top_layout)
    View mapTopLayout;
    @Bind(R.id.maplayout_search)
    ImageView topSearchBtn;


    private boolean isEndPositionEnadle = false;

    private ImageView topBackBtn;
    private ImageView topHomeBtn;
    private TextView topTitle;
    private MarkerOverlay markerOverlay;
    private List<FloorModel> floorModels;
    private List<String> itemNames;
    private GridViewAdapter adapter;
    private List<RecordBean> searchRecords;
    private POIData startPoiData;
    private POIData endPoiData;
    private SearchResultAdapter searchResultAdapter;
    private List<Feature> features;
    private MallModel mallModel;
    private InputMethodManager imm;
    private boolean search = false;
    private int backFlag = 0;
    private CompassOverlay compassOverlay;
//    @Bind(R.id.point_direction)
//    View compass;


    HighLightOverlay highLightOverlay;

    Handler handler = new Handler();

    @Override
    public void initView() {
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        //添加消息处理
        Intent intent = getIntent();
        mallModel = intent.getParcelableExtra("MallModel");
        floorModels = intent.getParcelableArrayListExtra("FloorModels");

        //2016.3.21 xun 设置头布局数据及监听事件
        initTopLayout();

        searchRecords = SharedPreferencesUtil.getSearchRecord(this);
        // 设置九宫格内容
        adapter = new GridViewAdapter(this, initData());
        //从SharedPrefrences查询结果
        tlvRecoed.setAdapter(new SearchRecordAdapter());
        gridview.setAdapter(this.adapter);
        features = new ArrayList<>();

        ltvNavigate.setOnTextItemClickListener(this);
        searchResultAdapter = new SearchResultAdapter(this);
        searchResult.setAdapter(searchResultAdapter);
        // 实例化业务接口
        presenter.setNexdMap(nexdMap);
        presenter.setMallModel(mallModel);
        initListItemData();
        ltvNavigate.setSelection(ListTextView.DEFAULT_SELECTED_ITEM);
        // 初始化地图
        presenter.initMap();
        nexdMap.registerMapViewClickListener(this);
        nexdMap.registerMapViewListener(this);
        nexdMap.registerMapViewOtherClickListener(this);
        // 初始化加载第一张地图
        if (floorModels != null && floorModels.size() != 0) {
            presenter.loadMap(floorModels.get(0));
        } else {
            showToast("获取楼层信息出错！");
        }
    }

    @Override
    public int getLayoutRes() {
        return R.layout.activity_map_show;
    }

    @Override
    public void initPresenter() {
        presenter = new NexdMapPresenter(this, this);
        presenter.init();
    }

    private void initTopLayout() {
        if (mapTopLayout.getVisibility() == View.GONE) {
            mapTopLayout.setVisibility(View.VISIBLE);
        }
        topBackBtn = (ImageView) mapTopLayout.findViewById(R.id.maplayout_back);
        topTitle = (TextView) mapTopLayout.findViewById(R.id.maplayout_title);
        topSearchBtn = (ImageView) mapTopLayout.findViewById(R.id.maplayout_search);
        topHomeBtn = (ImageView) mapTopLayout.findViewById(R.id.maplayout_home);
        topTitle.setText(mallModel.getMallCName());
        goToThere.setVisibility(View.GONE);
        if (!MyApplication.IS_ROUTE_PLAN) {
            startPosition.setVisibility(View.GONE);
            endPosition.setVisibility(View.GONE);
            topSearchBtn.setVisibility(View.GONE);
        }
    }


    //当AdapterView被单击(触摸屏或者键盘)，则返回的Item单击事件
    @OnItemClick(R.id.gridview)
    public void onGridViewItemClicked(int position) {

        GridViewModel gridViewModel = NexdMapActivity.this.adapter.getItem(position);
        FloorsFeature floorsFeature = NexdMapActivity.this.nexdMap.getFloorFeature();
        List<Map<Long, Feature>> featureList = floorsFeature.getFeatures();
        features.clear();
        for (Map<Long, Feature> features : featureList) {
            for (Feature feature : features.values()) {
                int spaceType = feature.getSpaceType();
                if (spaceType == 99 || spaceType == -1) {
                    continue;
                }
                if (position < 3) {
                    String itemCode = gridViewModel.getItemCode();
                    String shortItemCode = itemCode.substring(0, 4);
                    String shortFeatureCode = String.valueOf(spaceType).substring(0, 4);
                    if (shortItemCode.equals(shortFeatureCode)) {
                        NexdMapActivity.this.features.add(feature);
                    }
                } else {
                    if (gridViewModel.getItemCode().equals(String.valueOf(spaceType))) {
                        NexdMapActivity.this.features.add(feature);
                    }
                }
            }
        }
        if (features.size() == 0) {
            showToast("未找到相应结果");
        } else {
            typeRecord.setVisibility(View.GONE);
            searchResult.setVisibility(View.VISIBLE);
            searchResultAdapter.addFeatures(NexdMapActivity.this.features);
        }
    }

    // 这里处理搜索到的结果的item点击事件
    @OnItemClick(R.id.search_result)
    public void onSearchResultItemClicked(int position) {
        searchLayout.setVisibility(View.GONE);
        popuLayout.setVisibility(View.VISIBLE);
        nexdMap.removeOverlay(MARKER);
        //隐藏软键盘 //
        imm.hideSoftInputFromWindow(back.getWindowToken(), 0);
        Feature feature = searchResultAdapter.getItem(position);
        endPoiData = new POIData();
        endPoiData.setCenter_x(feature.getCenter_x());
        endPoiData.setCenter_y(feature.getCenter_y());
        poiName.setText(feature.getName());
        endPoiData.setPoi_id(feature.getFeature_id());
        endPoiData.setFloor_id(presenter.getCurrentFloorId());
        addNexdMarker(new PointF(endPoiData.getCenter_x(), endPoiData.getCenter_y()), R.drawable.icon_marker, MARKER);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mall, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @OnTextChanged(value = R.id.search_content, callback = OnTextChanged.Callback.TEXT_CHANGED)
    public void onTextChange(CharSequence character) {
        String content = character.toString();
        if (TextUtils.isEmpty(content)) {
            searchResultAdapter.clear();
            typeRecord.setVisibility(View.VISIBLE);
            searchResult.setVisibility(View.GONE);
            searchClear.setVisibility(View.INVISIBLE);
        } else {
            typeRecord.setVisibility(View.GONE);
            searchResult.setVisibility(View.VISIBLE);
            features = nameBlurredMatch(content);
            searchClear.setVisibility(View.VISIBLE);

            searchResultAdapter.addFeatures(features);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_compose:
                startActivity(new Intent(this, AboutmeActivity.class));
                break;
            case android.R.id.home:
                search = false;
                this.finish();
                break;

            case R.id.search_bar:
                if (searchResultAdapter.getCount() != 0) {
                    typeRecord.setVisibility(View.GONE);
                    searchResult.setVisibility(View.VISIBLE);
                    searchResultAdapter.notifyDataSetChanged();
                }
                searchLayout.setVisibility(View.VISIBLE);
                mapLayout.setVisibility(View.GONE);
                popuLayout.setVisibility(View.GONE);
                break;
            default:

                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initListItemData() {
        if (itemNames == null) {
            itemNames = new ArrayList<>();
        }

        itemNames.clear();
        for (FloorModel floorModel : floorModels) {
            itemNames.add(floorModel.getFloorName());
        }

        ltvNavigate.setItems(itemNames);
    }

    @Override
    public void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(NexdMapActivity.this, msg, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void showProgress(int visibility) {

    }

    @Override
    public void printLog(String tag, String msg, int type) {

    }


    @Override
    public void loadMap(String svgPath) {
        try {
            nexdMap.loadMap(new FileInputStream(new File(svgPath)));
        } catch (Exception e) {
            MyApplication.getInstance().reportException(e);
        }
    }

    @Override
    public void setCurrentFloor(String svgPath) {
        try {
            searchContent.setText("");
            nexdMap.setCurrentFloor(new FileInputStream(new File(svgPath)), this);
            nexdMap.registerMapViewClickListener(this);
            popuLayout.setVisibility(View.GONE);
        } catch (FileNotFoundException e) {
            MyApplication.getInstance().reportException(e);
        }
    }

    @Override
    public void addOverlay(SVGMapBaseOverlay baseOverlay, int type) {
        nexdMap.addOverlay(baseOverlay, type);
    }

    @Override
    public void resetRoutePlan() {
        startPoiData = null;
        isEndPositionEnadle = false;
        endPoiData = null;
        startPosition.setVisibility(View.VISIBLE);
        endPosition.setVisibility(View.VISIBLE);
        goToThere.setVisibility(View.GONE);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        presenter.startLocation();
    }

    public void onItemClickListener(int which, String itemName) {
        presenter.setCurrentFloor(floorModels.get(which));
    }

    @OnClick({R.id.maplayout_back, R.id.back, R.id.maplayout_search, R.id.maplayout_home, R.id.search_go, R.id.search_clear, R.id.gotothere, R.id.start_position, R.id.end_position/*, R.id.point_direction*/})
    public void onBtnClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                if (searchResultAdapter.getCount() != 0) {
                    typeRecord.setVisibility(View.VISIBLE);
                    searchResult.setVisibility(View.GONE);
                    searchResultAdapter.clear();
                } else {
                    mapTopLayout.setVisibility(View.VISIBLE);
                    searchLayout.setVisibility(View.GONE);
                    mapLayout.setVisibility(View.VISIBLE);
                }
                popuLayout.setVisibility(View.VISIBLE);
                imm.hideSoftInputFromWindow(back.getWindowToken(), 0);
                break;
            case R.id.maplayout_back:
                Intent intent = new Intent(this, MallListActivity.class);
                startActivity(intent);
                break;
            case R.id.search_go:
                if (searchResultAdapter.getCount() == 0) {
                    Toast.makeText(this, "墨墨没有找到您想要去的小店~", Toast.LENGTH_SHORT).show();
                    return;
                }
                search = true;
                imm.hideSoftInputFromWindow(back.getWindowToken(), 0);
                nexdMap.removeOverlay(MARKER);
                searchLayout.setVisibility(View.GONE);
                popuLayout.setVisibility(View.GONE);
                mapTopLayout.setVisibility(View.VISIBLE);
                mapLayout.setVisibility(View.VISIBLE);
                addNexdMarkers(searchResultAdapter.getPointF(), R.drawable.icon_marker, MARKER);
                break;
            case R.id.maplayout_search:
                backFlag = 0;
                mapTopLayout.setVisibility(View.GONE);
                if (searchResultAdapter.getCount() != 0) {
                    typeRecord.setVisibility(View.GONE);
                    searchResult.setVisibility(View.VISIBLE);
                    searchResultAdapter.notifyDataSetChanged();
                }
                searchLayout.setVisibility(View.VISIBLE);
                mapLayout.setVisibility(View.GONE);
                popuLayout.setVisibility(View.GONE);
                break;
            case R.id.maplayout_home:
                startActivity(new Intent(this, AboutmeActivity.class));
                break;
            case R.id.search_clear:
                searchContent.setText("");
                break;
            case R.id.gotothere:
                if (startPoiData == null) {

                    if (endPoiData != null && IS_LOCATIONING) {
                        nexdMap.removeOverlay(MARKER);
                        presenter.routePlanXY2POI(endPoiData);
                        popuLayout.setVisibility(View.GONE);
                        search = false;
                    } else {
                        showToast("定位中...");
                    }
                } else {
                    nexdMap.removeOverlay(MARKER);
                    presenter.routePlanPOI2POI(startPoiData, endPoiData);
                    popuLayout.setVisibility(View.GONE);
                    search = false;
                }
                break;

            case R.id.start_position:
                startPoiData = endPoiData;
                startPosition.setVisibility(View.GONE);
                popuLayout.setVisibility(View.GONE);
                break;

            case R.id.end_position:
                isEndPositionEnadle = true;
                startPosition.setVisibility(View.GONE);
                endPosition.setVisibility(View.GONE);
                goToThere.setVisibility(View.VISIBLE);
                break;
//            case R.id.point_direction:
//                nexdMap.setCurrentRotationDegrees(0, 0, 0);
//                break;

        }

    }

    // 获取地图截图
    public void getMapView(View view) {
        nexdMap.getCurrentMap();
    }

    // 地图加载监听事件
    @Override
    public void onMapLoadComplete(long taskId, final String buildName) {
        if (nexdMap.isMapLoadFinsh()) {


//            for (Map<Long, Feature> longFeatureMap : nexdMap.getFloorFeature().getFeatures()) {
//                for (Map.Entry entry : longFeatureMap.entrySet()) {
//                    if (highLightOverlay == null) {
//                        highLightOverlay = new HighLightOverlay((Feature) entry.getValue());
//                    } else {
//                        highLightOverlay.resetFeature((Feature) entry.getValue());
//                    }
//
////                    nexdMap.addOverlay(highLightOverlay, MARKER);
//                    handler.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            nexdMap.addOverlay(highLightOverlay, MARKER);
//                        }
//                    },2000);
////                    handler.sendEmptyMessage(21);
//                }
//            }


            runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    if (compassOverlay == null) {
//                        compassOverlay = new CompassOverlay(NexdMapActivity.this, compass, nexdMap);
//                    }
//                    nexdMap.addOverlay(compassOverlay, MARKER);
                    ProgressDialogUtils.dismissProgressDialog(NexdMapActivity.this);
                    Log.d("MySvgMapViewListener", "地图加载完成");
                    presenter.startLocation();
                }
            });


            while (true) {
                Random r = new Random();
                int index = r.nextInt(nexdMap.getFloorFeature().getFeatures().size() - 1);

                Map<Long, Feature> longFeatureMap = nexdMap.getFloorFeature().getFeatures().get(index);
                for (Map.Entry entry : longFeatureMap.entrySet()) {
                    if (highLightOverlay == null) {
                        highLightOverlay = new HighLightOverlay((Feature) entry.getValue());
                    } else {
                        highLightOverlay.resetFeature((Feature) entry.getValue());
                    }

//                    nexdMap.addOverlay(highLightOverlay, MARKER);
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            nexdMap.addOverlay(highLightOverlay, MARKER);
                        }
                    }, 2000);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    handler.sendEmptyMessage(21);
                }
            }

        } else {
            Log.d("MySvgMapViewListener", "加载未完成");
        }
    }

    @Override
    public void onMapLoadError() {
        showToast("地图加载失败，请检查您的数据!");
    }

    @Override
    public void onGetCurrentMap(Bitmap bitmap) {
        presenter.saveNexdMap(bitmap);
    }

    // 应用stop 停止采集定位
    @Override
    protected void onStop() {
        presenter.stopLocation();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        nexdMap.destroyDrawingCache();
//        nexdMap.onDestroy();
        presenter.destory();
        presenter.stopLocation();
        super.onDestroy();
    }

    // 初始化九宫格内容
    private ArrayList<GridViewModel> initData() {
        ArrayList<GridViewModel> gridViewModels = new ArrayList<>();
        gridViewModels.add(new GridViewModel(R.drawable.mark_type_selected_clothing, "服装", "101510"));
        gridViewModels.add(new GridViewModel(R.drawable.mark_type_selected_food, "美食", "101010"));
        gridViewModels.add(new GridViewModel(R.drawable.shopping, "超市", "102210"));
        gridViewModels.add(new GridViewModel(R.drawable.mark_type_selected_coffee, "咖啡", "101014"));
        gridViewModels.add(new GridViewModel(R.drawable.mark_type_selected_ktv, "KTV", "101111"));
        gridViewModels.add(new GridViewModel(R.drawable.mark_type_selected_movies, "影院", "101112"));
        gridViewModels.add(new GridViewModel(R.drawable.mark_type_selected_parking, "停车场", "102513"));
        gridViewModels.add(new GridViewModel(R.drawable.toilet, "卫生间", "102515"));
        return gridViewModels;
    }

    // 按照名字匹配 获取feature
    private List<Feature> nameBlurredMatch(String name) {
        List<Feature> featureRtn = new ArrayList<>();
        FloorsFeature floorsFeature = NexdMapActivity.this.nexdMap.getFloorFeature();
        List<Map<Long, Feature>> featureList = floorsFeature.getFeatures();
        for (Map<Long, Feature> featureMap : featureList) {
            for (Feature feature : featureMap.values()) {
                if (feature.getName().contains(name)) {
                    featureRtn.add(feature);
                }
            }
        }
        return featureRtn;
    }

    // 地图poi的点击事件
    @Override
    public void onClick(float screenX, float screenY, float mapX, float mapY, POIData poiData) {
        if (isEndPositionEnadle) {
            return;
        }
        if (!search) {
            nexdMap.removeOverlay(MARKER);
            addNexdMarker(new PointF(poiData.getCenter_x(), poiData.getCenter_y()), R.drawable.icon_marker, MARKER);
        }
        popuLayout.setVisibility(View.VISIBLE);
        poiName.setText(poiData.getName());
        this.endPoiData = poiData;
    }

    // 搜索结果列表的item点击事件, 点击显示地图以及坐标点
    @OnItemClick(R.id.search_result)
    public void onSearchResultClicked(int position) {
        searchLayout.setVisibility(View.GONE);
        mapLayout.setVisibility(View.VISIBLE);
        mapTopLayout.setVisibility(View.VISIBLE);
        addNexdMarker(new PointF(searchResultAdapter.getItem(position).getCenter_x(),
                        searchResultAdapter.getItem(position).getCenter_y()),
                R.drawable.icon_marker,
                MARKER);
    }

    // 向地图添加一堆Mark
    private void addNexdMarkers(List<PointF> pointFs, int res, int type) {
        MorePointOverly morePointOverly = new MorePointOverly(this);
        morePointOverly.setRes(res);
        morePointOverly.setPositions(pointFs);
        nexdMap.addOverlay(morePointOverly, type);
    }

    // 向地图添加一个mark
    private void addNexdMarker(PointF pointF, int res, int type) {
        if (markerOverlay == null) {
            markerOverlay = new MarkerOverlay(this);
        }
        markerOverlay.setPosition(pointF);
        markerOverlay.setRes(res);
        nexdMap.addOverlay(markerOverlay, type);
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        if (searchLayout.getVisibility() == View.VISIBLE && backFlag == 0) {
            searchLayout.setVisibility(View.GONE);
            mapTopLayout.setVisibility(View.VISIBLE);
            mapLayout.setVisibility(View.VISIBLE);
            popuLayout.setVisibility(View.VISIBLE);
            backFlag = 1;
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void onClick(float v, float v1, float v2, float v3) {
        if (!isEndPositionEnadle) {

            popuLayout.setVisibility(View.GONE);
            nexdMap.removeOverlay(MARKER);
        }
    }


    static class ViewHolder {
        TextView textView;
    }

    private class SearchRecordAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return searchRecords.size();
        }

        @Override
        public RecordBean getItem(int position) {
            return searchRecords.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;

            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = FrameLayout.inflate(NexdMapActivity.this, android.R.layout.simple_dropdown_item_1line, null);
                viewHolder.textView = (TextView) convertView.findViewById(android.R.id.text1);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText(getItem(position).getRecord());
            return convertView;
        }
    }

}